#import "WechatPlugin.h"

static NSString *callbackId;
static WechatPlugin *instance;

@implementation WechatPlugin

- (void)login:(CDVInvokedUrlCommand *)command{
    [WXApi registerApp:[command.arguments objectAtIndex:0]];
    if([WXApi isWXAppInstalled]){
        instance = self;
        SendAuthReq *req = [[SendAuthReq alloc] init];
        req.scope = @"snsapi_userinfo";
        req.state = @"App";
        [WXApi sendReq:req];
    } else {
        NSLog(@"BeanYon: no wechat");
        CDVPluginResult *commandResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:@"no wechat"];
        [self.commandDelegate sendPluginResult:commandResult callbackId:command.callbackId];
    }
    callbackId = command.callbackId;
}

- (void)share:(CDVInvokedUrlCommand *)command{
    [WXApi registerApp:[command.arguments objectAtIndex:0]];
    if([WXApi isWXAppInstalled]){
        instance = self;
        WXMediaMessage *message = [WXMediaMessage message];
        message.title = [command.arguments objectAtIndex:3];
        message.description = [command.arguments objectAtIndex:4];
        [message setThumbImage: [UIImage imageNamed:@"Overlay.png"]];
        WXWebpageObject *webpageObject = [WXWebpageObject object];
        webpageObject.webpageUrl = [command.arguments objectAtIndex:2];
        message.mediaObject = webpageObject;
        SendMessageToWXReq *req = [[SendMessageToWXReq alloc] init];
        req.bText = NO;
        req.message = message;
        if([[command.arguments objectAtIndex:5] isEqualToString:@"true"]){
            req.scene = WXSceneTimeline;
        } else {
            req.scene = WXSceneSession;
        }
        
        NSLog(@"BeanYon share : %@", [command.arguments objectAtIndex:5]);
        
        [WXApi sendReq:req];
    } else {
        NSLog(@"BeanYon: no wechat");
        CDVPluginResult *commandResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:@"no wechat"];
        [self.commandDelegate sendPluginResult:commandResult callbackId:command.callbackId];
    }
    callbackId = command.callbackId;
}

- (void)pay:(CDVInvokedUrlCommand *)command{
    [WXApi registerApp:[command.arguments objectAtIndex:0]];
    if([WXApi isWXAppInstalled]){
        instance = self;
        PayReq *req = [[PayReq alloc] init];
        req.partnerId = [command.arguments objectAtIndex:1];
        req.prepayId = [command.arguments objectAtIndex:2];
        req.package = @"Sign=WXPay";
        req.nonceStr = [command.arguments objectAtIndex:4];
        req.timeStamp = [[command.arguments objectAtIndex:5] intValue];
        req.sign = [command.arguments objectAtIndex:3];
        [WXApi sendReq:req];
    } else {
        NSLog(@"BeanYon: no wechat");
        CDVPluginResult *commandResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:@"no wechat"];
        [self.commandDelegate sendPluginResult:commandResult callbackId:command.callbackId];
    }
    callbackId = command.callbackId;
}


- (void)backToJs:(NSString *)code{
    CDVPluginResult *result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:code];
    [instance.commandDelegate sendPluginResult:result callbackId:callbackId];
}

@end
